﻿using System;
using System.Data;
using System.Reflection;

namespace UtilityLibrary.Ormapping
{
    /// <summary>
    /// 实体关系映射对象操作类
    /// </summary>
    public sealed class ModelAttributeOperator
    {
        /// <summary>
        /// 私有化构造函数
        /// </summary>
        private ModelAttributeOperator() { }

        #region 获取元数据信息

        /// <summary>
        /// 获得类型的属性描述信息
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static ModelClassAttribute GetModelClassAttribute(MemberInfo type)
        {
            //获得实体类描述信息
            Object[] cAtts = type.GetCustomAttributes(typeof(ModelClassAttribute), false);
            if (cAtts.Length <= 0)
            {
                return new ModelClassAttribute(type.Name);
            }//throw new OrmappingModelException("实体类没有表名描述。");
            return cAtts[0] as ModelClassAttribute;
        }

        /// <summary>
        /// 获得属性的属性描述信息
        /// </summary>
        /// <param name="property">属性</param>
        /// <returns></returns>
        public static ModelPropertyAttribute GetModelPropertyAttribute(MemberInfo property)
        {
            //获取属性的表字段信息
            Object[] atts = property.GetCustomAttributes(typeof(ModelPropertyAttribute), false);
            if (atts.Length <= 0)
            {
                return new ModelPropertyAttribute(property.Name, property.Name.ToLower() == "id", property.Name.ToLower() == "id");
            }//throw new OrmappingModelException("属性没有字段名称描述。");
            return atts[0] as ModelPropertyAttribute;
        }

        #endregion
    }
}
